//
//  SexViewController.swift
//  newShaohuoRen
//
//  Created by hujinbiao on 2018/3/24.
//  Copyright © 2018年 hujinbiao. All rights reserved.
//

import UIKit
import SwiftProgressHUD

protocol SexViewControllerDelegate {
    func sexViewController(sex:String)
}

class SexViewController: UITableViewController {
    
    var selIndex : IndexPath!
    
    var sexStr : String!
    
    var delegate : SexViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "性别"
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(saveBtnAction))
        
        if sexStr == "保密"{
            selIndex = IndexPath(row: 0, section: 0)
        }else if sexStr == "男" {
            selIndex = IndexPath(row: 1, section: 0)
        }else {
            selIndex = IndexPath(row: 2, section: 0)
        }
        tableView.selectRow(at: selIndex, animated: true, scrollPosition: UITableViewScrollPosition.top)
        let indexPath = tableView.indexPathForSelectedRow
        let cell = tableView.cellForRow(at: indexPath!)
        cell?.accessoryType = .checkmark
        
    }
    
    @objc func saveBtnAction() -> Void {
        
        if selIndex == nil {
            SwiftProgressHUD.showOnlyText("请选择性别")
            return
        }
        
        requestUpdateUserInfo(sex: "\(selIndex.row)")
    }
    
    func requestUpdateUserInfo(sex:String) -> Void {
        
        //要加密的数据
        let param = ["command":"updateUserInfo","uid":UserTicketModel.sharedInstance.uid ?? "","token":UserTicketModel.sharedInstance.token ?? "","sex":sex,"app_name":"shr"]
        
        NetworkTools.sharedInstance.postRequest(urlString: "", parameters: param as AnyObject, headers: ["copyright":"v1","interface":"UserInfo"], successed: { (resultDict) in
            
            SwiftProgressHUD.showOnlyText("操作成功")
            let data = resultDict["data"] as! [String:AnyObject]
            let sexStr = data["sex"] as! String
            if sexStr == "0"{
                self.delegate?.sexViewController(sex: "保密")
            }else if sexStr == "1" {
                self.delegate?.sexViewController(sex: "男")
            }else {
                self.delegate?.sexViewController(sex: "女")
            }
            self.navigationController?.popViewController(animated: true)
        })
    }
}

extension SexViewController {
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if selIndex != nil {
            let cell = tableView.cellForRow(at: selIndex)
            cell?.accessoryType = .none
        }
        
        let cell = tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .checkmark
        
        selIndex = indexPath
    }
}
